﻿namespace MDXStudio
{
    using MDXParser;
    using System;
    using System.Collections.Generic;
    using System.Text;

    internal class DebugWatch
    {
        private MDXNode m_Set;
        private List<DebugWatchItem> m_Watches = new List<DebugWatchItem>();

        internal DebugWatch()
        {
        }

        internal string BuildMDX(MDXSelectNode select, string cube)
        {
            StringBuilder sb = new StringBuilder("WITH ");
            if (select != null)
            {
                sb.Append(select.Withs.GetDebugMDX());
            }
            foreach (DebugWatchItem item in this.m_Watches)
            {
                item.BuildMDX(sb);
            }
            sb.Append(" SELECT {");
            bool flag = true;
            foreach (DebugWatchItem item2 in this.m_Watches)
            {
                if (!flag)
                {
                    sb.Append(",");
                }
                sb.Append(item2.UniqueName);
                flag = false;
            }
            sb.Append("} ON 0 ");
            if (this.m_Set != null)
            {
                sb.AppendFormat(", {0} ON 1 ", this.m_Set.GetDebugMDX());
            }
            sb.AppendFormat(" FROM [{0}]", cube);
            return sb.ToString();
        }

        internal void Clear()
        {
            this.m_Set = null;
            this.m_Watches.Clear();
        }

        private bool Compatible(MDXExpNode e, string parentcmname)
        {
            if ((e.m_OuterIterator != null) && (e.m_OuterIterator != this.m_Set))
            {
                return false;
            }
            MDXDataType mDXType = e.GetMDXType();
            if (MDXDataType.Set == mDXType)
            {
                return false;
            }
            foreach (DebugWatchItem item in this.m_Watches)
            {
                if (item.m_ParentCMName != parentcmname)
                {
                    return false;
                }
            }
            return true;
        }

        internal void Update(MDXExpNode e, string parentcmname, string cmname, bool watchObject)
        {
            if (!this.Compatible(e, parentcmname))
            {
                this.Clear();
            }
            if (e.m_OuterIterator != null)
            {
                this.m_Set = e.m_OuterIterator;
            }
            if (e.GetMDXType() == MDXDataType.Set)
            {
                this.m_Set = e;
            }
            else
            {
                int num = 0;
                bool flag = false;
                string str = cmname;
                while (!flag)
                {
                    flag = true;
                    foreach (DebugWatchItem item in this.m_Watches)
                    {
                        if (item.m_CMName.Equals(str, StringComparison.InvariantCultureIgnoreCase))
                        {
                            flag = false;
                            str = string.Format("{0} ({1})", cmname, num);
                            num++;
                            continue;
                        }
                    }
                }
                DebugWatchItem item2 = new DebugWatchItem();
                item2.m_CMName = str;
                item2.m_ParentCMName = parentcmname;
                item2.m_Exp = e;
                item2.m_watchObject = watchObject;
                this.m_Watches.Add(item2);
            }
        }
    }
}

